package com.neutech.neureg.service.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neutech.neureg.mapper.InvoiceMapper;
import com.neutech.neureg.mapper.PatientCostsMapper;
import com.neutech.neureg.mapper.RegisterMapper;
import com.neutech.neureg.service.RegisterService;
import com.neutech.po.Register;
import com.neutech.vo.ViewObject;

@Service
public class RegisterServiceImpl implements RegisterService{
	@Autowired
	private RegisterMapper registerMapper;
	@Autowired
	private InvoiceMapper invoiceMapper;
	@Autowired
	private PatientCostsMapper patientCostsMapper;
	@Override
	/**
	 * 添加挂号信息成功前，需自动生成发票号和病历号
	 * @param:可以装实体Bean的Map类型参数
	 * @return：1--success；0--failure
	 */
	public int addRegisterInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(new Date()).toString();
		map.put("birthDate", dateStr);//设置birthDate格式
		map.put("visitDate", dateStr);//设置visitDate格式
		//插入register表
		int reg = registerMapper.insertIntoRegister(map);
		//获取register表中记录的最大id
		int maxRegID = registerMapper.getMaxRegID();
		//插入发票表,并获取发票表中的最大ID
		Map<String,Object> invoice = new HashMap<String,Object>();
		invoice.put("InvoiceNum", map.get("invoiceNumber"));
		invoice.put("Money", map.get("expensesReceivable"));
		invoice.put("State", 3);
		invoice.put("CreationTime",map.get("registTime"));
		invoice.put("UserID", map.get("userID"));
		invoice.put("RegistID",map.get("registID"));
		invoice.put("FeeType", map.get("constantPayment"));
		invoiceMapper.insertIntoInvoice(invoice);
		int maxInvID = invoiceMapper.getMaxInvID();
		//插入患者费用表
		Map<String,Object> patientCosts = new HashMap<String,Object>();
		patientCosts.put("RegistID", maxRegID);
		patientCosts.put("InvoiceID", maxInvID);
		patientCosts.put("ItemID", 1);
		patientCosts.put("ItemType", 1);
		patientCosts.put("Name", "挂号费用");
		patientCosts.put("Price", map.get("expensesReceivable"));//应收金额
		patientCosts.put("Amount", 1);//
		patientCosts.put("DeptID", map.get("deptID"));//科室ID
		patientCosts.put("CreationTime", map.get("registTime"));//开立时间
		patientCosts.put("CreatOperID", map.get("registID"));//开立医生ID
		patientCosts.put("PayTime", map.get("RegistTime"));//收费时间
		patientCosts.put("RegisterID", map.get("registerID"));//挂号ID
		patientCosts.put("FeeType", map.get(" constantPayment"));//收费方式
		patientCostsMapper.insertIntoPatientCosts(patientCosts);
		return reg;	
	}
	
	@Override
	public int insertIntoRegister(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return registerMapper.insertIntoRegister(map);
	}
	@Override
	public int getMaxRegID() {
		// TODO Auto-generated method stub
		return registerMapper.getMaxRegID();
	}
	@Override
	public Register getRegisterBean() {
		// TODO Auto-generated method stub
		return registerMapper.getRegisterBean();
	}
   

}
